package com.an.nopro.app.framework.controller;

import java.security.Principal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import com.an.nopro.app.framework.entity.WiselyMessage;
import com.an.nopro.app.framework.entity.WiselyResponse;

/**
 * @author Administrator
 *
 */
@Controller
public class WsController {

	/**
	 * 向浏览器发送消息
	 */
	@Autowired
	private SimpMessagingTemplate messagingTemplate;

	
	/**
	 * @param principal 包含当前用户信息
	 * @param msg
	 */
	@MessageMapping("/chat")
	public void handleChat(Principal principal, String msg) {
		// 硬编码 发送人是anzi,则发送给zhang 
		if (principal.getName().equals("anzi")) {
			//向用户发送消息(接收消息的用户,浏览器订阅的地址，消息本身)
			messagingTemplate.convertAndSendToUser("zhang", "/queue/notification",
					principal.getName() + "-send:" + msg);
		}
	}

	@MessageMapping("/welcome") // 向浏览器发送请求，通过这个映射地址
	@SendTo("/topic/getResponse") // 当服务端有消息时，会对订阅了@SendTo中路径的浏览器发送消息
	public WiselyResponse say(WiselyMessage message) throws InterruptedException {
		return new WiselyResponse("welcome," + message.getName() + "!当前时间：" + new Date());
	}
}
